package com.jzg.jcptadmin.view;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Point;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.NumberPicker;

import com.jzg.jcptadmin.R;
import com.jzg.jcptadmin.Utils.StringUtils;
import com.jzg.jcptadmin.base.BaseActivity;

/**
 * 选择时间界面
 * 
 * @author 作者 E-mail:
 * @version 创建时间：
 */
@SuppressLint("NewApi")
public class TimeSelectActivity extends BaseActivity {

	
	int win_width ;
    int win_height;
    NumberPicker yearNumberPicker;
    NumberPicker monthNumberPicker;
    
    
    private int maxYear;
    private int minYear=1990;
    
    private int maxMonth = 12;
    private int minMonth = 1;

    private int currentMonth;
	private int currentYear;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 隐藏标题
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		// 设置全屏
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		
		setFinishOnTouchOutside(true);

		setContentView(R.layout.aty_sheettime);
		
		yearNumberPicker = (NumberPicker)findViewById(R.id.yearNumberPicker);
		monthNumberPicker = (NumberPicker)findViewById(R.id.monthNumberPicker);
		WindowManager m = getWindowManager();
		Display d = m.getDefaultDisplay(); // 为获取屏幕宽、高
		WindowManager.LayoutParams p = getWindow().getAttributes(); // 获取对话框当前的参数值
		Point size = new Point();
		d.getSize(size);
		win_width = size.x;
		win_height = size.y;
		p.gravity = Gravity.BOTTOM;
		p.height = (int) (win_height * 0.5); // 高度设置为屏幕的0.6
		p.width = (int) (win_width * 1); // 宽度设置为屏幕的1
		p.alpha = 1.0f;// 设置透明度
				
		maxYear = getIntent().getIntExtra("Maxyear", 2016);
		minYear = getIntent().getIntExtra("Minyear", 2006);
		maxMonth = getIntent().getIntExtra("MaxMonth", 12);
		minMonth = getIntent().getIntExtra("MinMonth", 1);
		currentMonth = getIntent().getIntExtra("CurMonth", 1);
		currentYear = maxYear;
		if(currentYear== StringUtils.getCurrent(StringUtils.TimeType.YEAR)){
			maxMonth = StringUtils.getCurrent(StringUtils.TimeType.MONTH);
		}else{
			maxMonth = 12;
		}
		
		this.getWindow().setAttributes(p);
		yearNumberPicker.setMaxValue (maxYear);
		yearNumberPicker.setMinValue(minYear);
		yearNumberPicker.setValue(maxYear);
		
		monthNumberPicker.setMaxValue(maxMonth);
		monthNumberPicker.setMinValue(minMonth);
		monthNumberPicker.setValue(currentMonth);
		
		yearNumberPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
		monthNumberPicker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
		yearNumberPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
			@Override
			public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
				currentYear = picker.getValue();
				if(currentYear== StringUtils.getCurrent(StringUtils.TimeType.YEAR)){
					maxMonth = StringUtils.getCurrent(StringUtils.TimeType.MONTH);
				}else{
					maxMonth = 12;
				}
				monthNumberPicker.setMaxValue(maxMonth);
				monthNumberPicker.setValue(1);
			}
		});
		monthNumberPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
			@Override
			public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
				currentMonth = picker.getValue();
				monthNumberPicker.setValue(currentMonth);
			}
		});
	}

	
	public void time_done(View v){
		back_data();
		this.finish();
	}
	
	
	public void back_data(){
		Intent in = new Intent();
		in.putExtra("year", yearNumberPicker.getValue());
		in.putExtra("month", monthNumberPicker.getValue());
		setResult(1000, in);
	}

}
